#include "Rectangle.h"
#include "Border.h"
#include "BorderChecker.h"
#include "PrecisionConvertor.h"
#include "AreaCalculator.h"
#include "PerimeterCalculator.h"

namespace
{

const unsigned int precision = 2;

void setBorder(Border& border, double value)
{
    double tempValue = border.getLength();
    border.setLength(value);
    if (!border.isValid()) border.setLength(tempValue);
}

}

Rectangle::Rectangle(double height, double width)
    : height_m(PrecisionConvertor::getResult(height, precision, false), 
               BorderChecker(0, false, 75, false)),
      width_m(PrecisionConvertor::getResult(width, precision, false), 
              BorderChecker(0, false, 100, true))
{
    return;
}

double Rectangle::getArea() const
{
    double value = AreaCalculator::getResult(height_m, width_m);
    return PrecisionConvertor::getResult(value, precision, true);
}

double Rectangle::getPerimeter() const
{
    double value = PerimeterCalculator::getResult(height_m, width_m);
    return PrecisionConvertor::getResult(value, precision, true);
}

void Rectangle::setHeight(double height)
{
    setBorder(height_m, height);
}

void Rectangle::setWidth(double width)
{
    setBorder(width_m, width);
}

bool Rectangle::isValid() const
{
    return ((height_m.isValid()) && (width_m.isValid()));
}


